﻿using System.Data.OleDb;
using System.Web.UI.WebControls;
using TraditionalMalteseCuisine.App_Code.Operations;

namespace TraditionalMalteseCuisine.App_Code.Functions
{
    public class Web_Functions
    {
        #region Load Web Controls
        public static void LoadComboBox(OleDbCommand command, DropDownList comboBox, string valueMember, string displayMember)
        {
            comboBox.Items.Clear();
            comboBox.AppendDataBoundItems = true;
            comboBox.Items.Insert(0, new ListItem(". . . Please select an item . . .", "0"));

            string value = ((comboBox.SelectedValue.Length > 0) ? comboBox.SelectedValue : string.Empty);

            comboBox.DataSource = ReadOp.GetDataTable(command);
            comboBox.DataTextField = displayMember;
            comboBox.DataValueField = valueMember;
            comboBox.DataBind();

            if (value.Length > 0)
                comboBox.SelectedValue = value;
            else
                comboBox.SelectedIndex = 0;
        }

        public static void LoadCheckedListBox(OleDbCommand command, CheckBoxList checkedListBox, string valueMember, string displayMember)
        {
            checkedListBox.Items.Clear();

            string value = ((checkedListBox.SelectedValue.Length > 0) ? checkedListBox.SelectedValue : string.Empty);

            checkedListBox.DataSource = ReadOp.GetDataTable(command);
            checkedListBox.DataTextField = displayMember;
            checkedListBox.DataValueField = valueMember;
            checkedListBox.DataBind();

            if (value.Length > 0)
                checkedListBox.SelectedValue = value;
        }
        #endregion
    }
}